// Copyright (c) 2023 Institute of Software, Chinese Academy of Sciences
// Ripple is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.

package ripple.server.tcp.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ripple.common.DebugTool;
import ripple.common.entity.Data;
import ripple.common.tcp.Handler;
import ripple.common.tcp.Message;
import ripple.common.tcp.message.PushRequest;
import ripple.common.tcp.message.PushResponse;

import ripple.server.core.Node;

import java.net.InetSocketAddress;

public class PushRequestHandler implements Handler {
    private static final Logger LOGGER = LoggerFactory.getLogger(PushRequestHandler.class);
    private Node node;

    private Node getNode() {
        return node;
    }

    private void setNode(Node node) {
        this.node = node;
    }

    public PushRequestHandler(Node node) {
        this.setNode(node);
    }

    @Override
    public Message handle(ChannelHandlerContext channelHandlerContext, Message message) {


        PushRequest pushRequest = (PushRequest) message;
        InetSocketAddress localAddress = ((NioSocketChannel) channelHandlerContext.channel()).localAddress();
//        InetSocketAddress remoteAddress = ((NioSocketChannel) channelHandlerContext.channel()).remoteAddress();

//        LOGGER.info("[PutRequestHandler] [{}:{}<-->{}:{}] Receive PUT request. UUID = {}, application name = {}, key = {}, value = {}."
//                , localAddress.getHostString(), localAddress.getPort(), remoteAddress.getHostString()
//                , remoteAddress.getPort(), pushRequest.getUuid(), pushRequest.getApplicationName()
//                , pushRequest.getKey(), pushRequest.getValue());

        DebugTool.write("push handle: node id: " + this.getNode().getId() + ", data: " + pushRequest.getData().toString());


        boolean result = this.getNode().push(pushRequest.getTopic(), pushRequest.getData());

        PushResponse pushResponse = new PushResponse();
        pushResponse.setUuid(pushRequest.getUuid());
        pushResponse.setSuccess(result);

//        LOGGER.info("[PutRequestHandler] [{}:{}<-->{}:{}] Send PUT response. Success = {}."
//                , localAddress.getHostString(), localAddress.getPort(), remoteAddress.getHostString()
//                , remoteAddress.getPort(), pushResponse.isSuccess());
        return pushResponse;
    }
}